#define __USB_PIC_PERIF__ 1

#include <18F4550.h>

#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN

#use delay(clock=48000000)

#use delay(clock=20000000)
#use rs232(baud=9600,bits=8,xmit=PIN_C6,rcv=PIN_C7)

#DEFINE USB_HID_DEVICE TRUE

/* This defines endpoint 1 to  INPUT transfers */
#define USB_EP1_TX_ENABLE USB_ENABLE_INTERRUPT
#define USB_EP1_TX_SIZE 8 

/* This defines endpoint 1 to OUTPUT transfers */
#define USB_EP1_RX_ENABLE USB_ENABLE_INTERRUPT
#define USB_EP1_RX_SIZE   8

#include <pic18_usb.h>
#include <usb_desc_hid.h>
#include <usb.c>

/* include of other source code */
#include "cell_manage.h"

/* defines of the code */
#define DATA_SIZE 50
#define COMM_SIZE 33
#define BUFF_SIZE 50

/* main output definition */
#define CLR PIN_B7

/* global variables */

/* declaration of methods */
void reset_config(void);
void enable_output(void);
int clear(char pin);
int inCi();

/* external methods */
extern int outCell(unsigned char, char);


/* main function */
void main(void) {

    /* Local variables */

    /* the total is 25 outputs */
    int16 outPuts[2], *pouts;
    char *pdata, dataIn[50];

    /* initialize variables */
    pdata = &dataIn[0];
    outPuts[0] = PIN_A0;
    outPuts[1] = '\0';

    pouts = &outPuts[0];

    reset_config(); 
    usb_init();
    
    /* debug    
    output_high(PIN_B6);
    delay_ms(1000);
    output_low(PIN_B6);
    */

   for(;;){         
      if(usb_enumerated()){
          reset_config();
         /* Test if there is data in the
          * buffer */
         if(usb_kbhit(1)){
            if(usb_get_packet(1,dataIn,sizeof((dataIn)+1))>=1){
               clear(CLR);
               pdata = &dataIn[0];
               outCi(pdata,pouts);
               delay_ms(2000);
           }
        }
      } 
   }
}


/* this function clear all OUTPUTS */
int clear(char pin){
   output_high(pin);
   output_low(pin); 
   return 0;
}


void reset_config(void){
   clear(CLR);
}




 